package com.acme;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PlayerSearch extends ListActivity {

	ArrayList<Player> results;
	TextView searchText;
	String searchTerm = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_name);
        results = new ArrayList<Player>();

        Button searchBtn = (Button)findViewById(R.id.searchBtn);
		searchText = (TextView)findViewById(R.id.searchText);
		
        if(savedInstanceState != null && savedInstanceState.containsKey("searchTerm")){
        	this.searchTerm = savedInstanceState.getString("searchTerm");
        	search(searchTerm);
        }
		
        searchBtn.setOnClickListener(new OnClickListener(){
        	
			public void onClick(View v) {
				//hide soft keyboard
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(searchText.getWindowToken(), 0);
				
				//kick off a loading dialog and make http request, later
				
				
				//for now:
				searchTerm = searchText.getText().toString();
				search(searchTerm);
			}
        	
        });
        
        
	}

	public void onListItemClick(ListView parent, View view, int position, long id) {
		Player selected = (Player)this.getListAdapter().getItem(position);
		Toast.makeText(getApplicationContext(), selected.getName(),
     	          Toast.LENGTH_SHORT).show();
	}
	
	//save instance state for rotations
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		// Save UI state changes to the savedInstanceState.
		// This bundle will be passed to onCreate if the process is
		// killed and restarted.
		savedInstanceState.putString("searchTerm", searchTerm);
		super.onSaveInstanceState(savedInstanceState);
	}
	
	public void search(String term){
		Player p = new Player(searchTerm);
		results.add(p);
		results.add(new Player("Dan Marino",Player.STATUS.Retired,Player.POSITION.QB, "Miami Dolphins", "Pittsburgh"));
        results.add(new Player("Vic Marino",Player.STATUS.Retired,Player.POSITION.OL, "Baltimore Colts", "Ohio State"));
        results.add(new Player("Todd Marinovich", Player.STATUS.Retired,Player.POSITION.QB, "Los Angeles Raiders", "Southern Cal"));
		Player[] players = new Player[results.size()];int i = 0;
        for(Player pl: results){
        	players[i] = pl;
        	i++;
        }
		setListAdapter(new PlayerListAdapter(PlayerSearch.this.getApplicationContext(), R.layout.player_list_item, players));
	}
	
	
}
